package oauthprovider

import (
	"context"

	"mall/common/modelx"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/backend/common/utils/pointy"
	"mall/service/setting/rpc/internal/svc"
	"mall/service/setting/rpc/internal/utils/errorhandler"
	"mall/service/setting/rpc/types/setting"

	"github.com/zeromicro/go-zero/core/logx"
)

type BackendGetOauthProviderByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBackendGetOauthProviderByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendGetOauthProviderByIdLogic {
	return &BackendGetOauthProviderByIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// group: oauthprovider
func (l *BackendGetOauthProviderByIdLogic) BackendGetOauthProviderById(in *setting.IDReq) (*setting.OauthProviderInfo, error) {
	if in.Id == 0 {
		return nil, errorx.NewInvalidArgumentError(i18n.ValidationError)
	}

	res, err := l.svcCtx.OauthProvider.FindOne(l.ctx, in.Id)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	return &setting.OauthProviderInfo{
		Id:           &res.ID,
		Name:         &res.Name,
		ClientId:     &res.ClientID,
		ClientSecret: &res.ClientSecret,
		RedirectUrl:  &res.RedirectURL,
		Scopes:       &res.Scopes,
		AuthUrl:      &res.AuthURL,
		TokenUrl:     &res.TokenURL,
		AuthStyle:    pointy.GetPointer(uint64(res.AuthStyle)),
		InfoUrl:      &res.InfoURL,
		Sort:         pointy.GetPointer(uint32(res.Sort)),
		Status:       pointy.GetPointer(uint32(res.Status)),
		CreatedAt:    pointy.GetPointer(modelx.FormatTime(res.CreatedAt, "")),
		UpdatedAt:    pointy.GetPointer(modelx.FormatTime(res.UpdatedAt, "")),
		DeletedAt:    pointy.GetPointer(modelx.FormatNullDotTime(res.DeletedAt, "")),
	}, nil
}
